var baseURL = "/knowledge/";
$(function() {
	
	Init(1);
	
	/*$(".recommend_list").click(function(){
		   var text = $(this).text();//这样无论你点哪一个div都 可以获它里面的值
		   $("input[name='searchName']").
		});*/
});
var pageNum = 1;
var serName;
var serType;
var pageCount;
var nowPage;

function Init(page_Num){
	var searchName = $("input[name='searchName']").val();
	var radio=document.getElementsByName("searchType");
    var searchType=null;   //  selectvalue为radio中选中的值
    for(var i=0;i<radio.length;i++){
        if(radio[i].checked==true) {
        	searchType=radio[i].value;
             break;
        }
    }
    var params = {
			"searchName":searchName,
			"searchType":searchType
	}
    
    $.post(baseURL + "/owlManage/getSemantics", {
    	"page":page_Num,
		"searchName" : searchName,
		"searchType" : searchType
	}, function(result) {
		if(result.msg=="success"){
			nowPage = page_Num;
			pageNum = result.page.pageNum;
			serName = searchName;
			serType = searchType;
			pageCount = result.page.pages;
			var kwList = result.page.list;
			if(kwList!=null && kwList.length>0){
				$("#file_List").html("");
				for(var i = 0;i<kwList.length;i++){
					var fileName = kwList[i].filename;
					var inputFile = kwList[i].saveurl;
					var documentId = kwList[i].doucmentId;
					if(fileName.length>15){
						fileName = fileName.substring(15, fileName.length);
						if(fileName.length>20){
							fileName = fileName.substring(0, 20)+"...";
						}
					}
					var url = inputFile;
					var inputFile=url;
					var newUrl = url.replace(/\\/g,"\/");
					var realUrl = baseURL + "localFile"+ newUrl.substr(12)
					var aItm=null;
					var index = realUrl.lastIndexOf(".");
					var ext = realUrl.substr(index+1).toLowerCase();
					
					realUrl = encodeURI(realUrl)
					if(i%2==0){
						var attrHtml = "";
						attrHtml+="<div class=\"filelist_value_div\">";
						attrHtml+="<div class=\"filelist_value\" onmouseover='showName("+kwList[i].filename+")'>"+fileName+"</div>";
						attrHtml+="<div class=\"filelist_value\">"+kwList[i].size+"KB</div>";
						attrHtml+="<div class=\"filelist_value\">"+kwList[i].uploadTime+"</div>";
						attrHtml+="<div class=\"filelist_value\">"+kwList[i].size+"</div>";
						if(ext=="pdf"){
							attrHtml+="<div class=\"filelist_value caozuo\"><a target='_blank' href="+ realUrl + ">查看</a></div>";
						}else{
							attrHtml+="<div class=\"filelist_value caozuo\"><a documentId="+documentId+" inputFile="+inputFile+" id='officetopdf' >查看</a></div>";
						}
						
						attrHtml+="</div>";
						$("#file_List").append(attrHtml);
					}else{
						var attrHtml = "";
						attrHtml+="<div class=\"filelist_value_div\">";
						attrHtml+="<div class=\"filelist_value1\" onmouseover='showName("+kwList[i].filename+")'>"+fileName+"</div>";
						attrHtml+="<div class=\"filelist_value1\">"+kwList[i].size+"KB</div>";
						attrHtml+="<div class=\"filelist_value1\">"+kwList[i].uploadTime+"</div>";
						attrHtml+="<div class=\"filelist_value1\">"+kwList[i].size+"</div>";
						if(ext=="pdf"){
							attrHtml+="<div class=\"filelist_value1 caozuo\"><a target='_blank' href="+ realUrl + ">查看</a></div>";
						}else{
							attrHtml+="<div class=\"filelist_value1 caozuo\"><a documentId="+documentId+" inputFile="+inputFile+" id='officetopdf' >查看</a></div>";
						}
						attrHtml+="</div>";
						$("#file_List").append(attrHtml);
					}
				}
			}
			
			$("#pages").html("");
			for (var int = 0; int < pageCount; int++) {
				var num = int+1;
				var pid = "page"+num;
				var attrHtml = "<div class=\"page_num\" id=\""+pid+"\">"+num+"</div>";
				$("#pages").append(attrHtml);
			}
			var pageId = "#page"+nowPage;
			$(pageId).css("color","#3366CC");
			
		}
	});
}



$("#search_Button").click(function(){
	Init(1);
})
function pageTo(num){
	Init(num);
}
$("#firstPage").click(function(){
	Init(1);
})
$("#previousPage").click(function(){
	if(pageNum>1){
		Init(pageNum-1);
	}
	
})
$("#nextPage").click(function(){
	if(pageNum<pageCount){
		Init(pageNum+1);
	}
	
})
$("#lastPage").click(function(){
	Init(pageCount);
})

function showFile(inputFile,documentId){
	var url=baseURL+"lookdocument/officeToPdf"
	var data={"inputFile":inputFile,"documentId":documentId}
	var newwindow=window.open("about:blank");
	$.post(url,data,function(r){
		debugger;
		console.log(r)
		var url=r.url
		var newUrl = url.replace(/\\/g,"\/");
		var realUrl = baseURL + "localFile"+ newUrl.substr(17)
		newwindow.location.href=realUrl;
        newwindow.focus();
	});
}

$(document).on("click","a[id='officetopdf']",function(){
	var athis=$(this)
	var documentId=parseInt(athis.attr("documentId"))
	var inputFile=athis.attr("inputFile")
	var url=baseURL+"lookdocument/officeToPdf"
	debugger
	var data={"inputFile":inputFile,"documentId":documentId}
	var newwindow=window.open("about:blank");
	$.post(url,data,function(r){
		debugger;
		console.log(r)
		var url=r.url
		var newUrl = url.replace(/\\/g,"\/");
		var realUrl = baseURL + "localFile"+ newUrl.substr(17)
		newwindow.location.href=realUrl;
        newwindow.focus();
	});
})
